import { getCollectionId } from '@/api/collection'
import { getRecordId } from '@/api/record'
import { getPrbUidFromId } from '@/api/common'
import { getPrbInfoFromSource } from '@/api/common'
import { getPrbInfoFromId } from '@/api/common'
import { getStatistics } from '@/api/statistics'



//判断是否是收藏题目
export async function isCollection(problemUid) {
  let problems = [];
  let collection = [];

  try {
    const res = await getCollectionId();
    collection = Object.values(res.data);

    for (var i = 0; i < collection.length; i++) {
      problems.push(collection[i].problemUid);
    }
    // console.log(problems)
    // console.log(problemUid)
    if (problems.includes(problemUid)) {
      return true;
    } else {
      return false;
    }
  } catch (error) {
    console.error(error);
    return false;
  }
}

//判断是否是历史记录中的题目
export async function isRecord(problemUid) {
  let problems = [];
  let records = [];
  let problemsDone = [];
  let problemsNotDone = [];

  try {
    const res = await getRecordId(problemUid);
    records = Object.values(res.data);


    for (var i = 0; i < records.length; i++) {
      problems.push(records[i].problemUid);
      // console.log(records[i].status)
      if (records[i].status === "not_done") {
        problemsNotDone.push(records[i].problemUid);
      }
      if (records[i].status === "done") {
        problemsDone.push(records[i].problemUid);
      }
    }
    // console.log(problems)
    // console.log(problemsDone)
    // console.log(problemsNotDone)
    // console.log(problemUid)

    if (!problems.includes(problemUid)) {
      return 1;
    } else if (problemsDone.includes(problemUid)) {
      return 2;
    } else if (problemsNotDone.includes(problemUid)) {
      return 3;
    }
    return 0;
  } catch (error) {
    console.log("Error in isRecord:", error);
    return 0; // 或者返回一个适当的错误状态码
  }
}

export function judgeTime() {
  const now = new Date();
  const hour = now.getHours();

  let timeDescription;

  if (hour >= 5 && hour < 9) {
    timeDescription = '早上好';
  } else if (hour >= 9 && hour < 12) {
    timeDescription = '上午好';
  } else if (hour >= 12 && hour < 14) {
    timeDescription = '中午好';
  } else if (hour >= 14 && hour < 18) {
    timeDescription = '下午好';
  } else if (hour >= 18 && hour < 22) {
    timeDescription = '晚上好';
  } else {
    timeDescription = '深夜了';
  }

  return timeDescription

}
//通过source+id获取problemUid
export function getProUid(source, id) {
  // console.log(source, id);
  return getPrbUidFromId(source, id).then((res) => {
    return res.data;
  });
}
//通过source+id获取题目信息
export function getProInfo(source, id) {
  // console.log(source, id);
  return getPrbInfoFromSource(source, id).then((res) => {
    // console.log(source, id)
    // console.log(res.data)
    return res.data;
  });
}
//通过uid获取题目信息
export function getProInfoFromUid(uid) {
  return getPrbInfoFromId(uid).then((res) => {
    // console.log(res.data)
    return res.data
  })
}

export function getTime() {
  // 创建一个 Date 对象，表示当前时间
  var currentDate = new Date();

  // 获取年、月、日、小时、分钟和秒
  var year = currentDate.getFullYear();
  var month = ('0' + (currentDate.getMonth() + 1)).slice(-2); // 月份从0开始，所以要加1
  var day = ('0' + currentDate.getDate()).slice(-2);
  var hours = ('0' + currentDate.getHours()).slice(-2);
  var minutes = ('0' + currentDate.getMinutes()).slice(-2);
  var seconds = ('0' + currentDate.getSeconds()).slice(-2);

  // 格式化为 yyyy-mm-dd HH:MM:SS 格式
  var formattedDate = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;

  return formattedDate
}
//通过用户信息获取做的最多的题目类型、题目难度、题目总数
export function getStatisticsForHome() {
  return getStatistics().then((res) => {
    var total = res.data.total
    const difficulty = Object.values(res.data.difficultyDistrb);
    const dNum = Math.max(...difficulty);
    let dName = Object.keys(res.data.difficultyDistrb)[difficulty.indexOf(dNum)]

    const tag = Object.values(res.data.topicDistrb);
    const tNum = Math.max(...tag)
    let tName = Object.keys(res.data.topicDistrb)[tag.indexOf(tNum)]

    const difficultyMapping = {
      "medium": "中等",
      "easy": "简单",
      "hard": "困难",
    };
    dName = difficultyMapping[dName] || '';

    const tagMapping = {
      "shuzu": "数组",
      "zifuchuan": "字符串",
      "haxi": "哈希",
      "shu": "树",
      "tu": "图",
      "dongtaiguihua": "动态规划",
      "shuxue": "数学",
      "paixu": "排序",
      "tanxin": "贪心",
    };

    tName = tagMapping[tName] || '';

    const result = {
      total: total,
      dNum: dNum,
      dName: dName,
      tNum: tNum,
      tName: tName
    };
    return result
  })
}
//通过用户信息获取statistics页面 三张饼图+两张柱形图所需信息
export function getStatisticsForStatistics() {
  return getStatistics().then((res) => {
    // console.log(res)
    const tagMapping = {
      "shuzu": "数组",
      "zifuchuan": "字符串",
      "haxi": "哈希",
      "shu": "树",
      "tu": "图",
      "dongtaiguihua": "动态规划",
      "shuxue": "数学",
      "paixu": "排序",
      "tanxin": "贪心",
    };
    const difficultyMapping = {
      "medium": "中等",
      "easy": "简单",
      "hard": "困难",
    };

    var fourx = Object.values(res.data.topicDistrb)

    var foury = Object.keys(res.data.topicDistrb)
    for (var i = 0; i < foury.length; i++) {
      foury[i] = tagMapping[foury[i]] || '';
    }

    var fivex = Object.values(res.data.difficultyDistrb)

    var fivey = Object.keys(res.data.difficultyDistrb)
    for (var i = 0; i < fivey.length; i++) {
      fivey[i] = difficultyMapping[fivey[i]] || '';
    }
    const result = {
      oneone: res.data.total,
      onetwo: 4194 - res.data.total,
      twoone: res.data.correctRate * res.data.total,
      twotwo: res.data.total * (1 - res.data.correctRate),
      threeone: res.data.days,
      threetwo: Math.floor(Math.random() * 10),
      fourx: fourx,
      foury: foury,
      fivex: fivex,
      fivey: fivey,

    };
    // console.log(result.twoone)
    // console.log(result.onetwo)
    return result
  })
}
export function getStatisticsForEvaluation() {
  return getStatistics().then((res) => {
    const tagMapping = {
      "shuzu": "数组",
      "zifuchuan": "字符串",
      "haxi": "哈希",
      "shu": "树",
      "tu": "图",
      "dongtaiguihua": "动态规划",
      "shuxue": "数学",
      "paixu": "排序",
      "tanxin": "贪心",
    };

    const arr = Object.entries(res.data.topicDistrb);
    arr.sort((a, b) => b[1] - a[1]);
    const topFive = arr.slice(0, 5);
    const tF = topFive.map(([key, value]) => ({
      key: tagMapping[key] || '',
      value
    }));

    let k = []
    let v = []
    let total = res.data.total
    for (var i = 0; i < tF.length; i++) {
      k.push(tF[i].key)
      v.push(tF[i].value)
      total = total - tF[i].value
    }

    k.push("其他")
    v.push(total)

    const result = {
      k: k,
      v: v,
      total: res.data.total
    };
    return result
  })
}
//以某数为最大数生成岁数
export function getRandom(maxValue) { {
    // 生成一个介于 0（包含）和 1（不包含）之间的随机小数
    const random = Math.random();

    // 将随机小数乘以最大值，然后向下取整得到随机整数
    const randomNumber = Math.floor(random * maxValue);

    return randomNumber;
  }
}
